
#include "shared_parameters/shared_params_interface.hpp"
#include <chrono>
#include <thread>
#include <iostream>
#include <csignal>

volatile sig_atomic_t stop = 0;

void signal_handler(int) {
    stop = 1;
}

int main() {
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    if (!SharedParamsInterface::initialize()) {
        std::cerr << "Writer: Failed to initialize shared memory!" << std::endl;
        return 1;
    }

    int counter = 0;
    while (!stop) {
        // 生成带时间戳的测试数据
        auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::system_clock::now().time_since_epoch()).count();

        int int_val = counter;
        double double_val = 0.1 * counter + timestamp/1e9;
        std::string str_val = "string_" + std::to_string(counter) + "_" + std::to_string(timestamp);
        bool bool_val = (counter % 2) == 0;

        // 原子写入操作
        SharedParamsInterface::setParam("test_node", "int_param", int_val);
        SharedParamsInterface::setParam("test_node", "double_param", double_val);
        SharedParamsInterface::setParam("test_node", "string_param", str_val);
        SharedParamsInterface::setParam("test_node", "bool_param", bool_val);
        SharedParamsInterface::setParam("sync", "counter", counter);

        std::cout << "Writer: Updated params (counter=" << counter << ")" << std::endl;
        counter++;

        std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 更快的写入频率
    }

    std::cout << "\nWriter: Cleanly exited after writing " << counter << times." << std::endl;
    return 0;
}